package pl.shenlon.ga.selection;

import pl.shenlon.ga.Population;
import pl.shenlon.ga.Specimen;

public class RouletteSpecimenSelector extends PopulationSpecimenSelector {

	public RouletteSpecimenSelector(Population population) {
		super(population);
	}

	@Override
	public Specimen[] select() {
		if (population.getScore() <= 0) {
			return population.getPopulation();
		}
		Specimen[] specimens = new Specimen[population.getPopulationSize()];

		for (int i = population.getElitarismRate(); i < specimens.length; i++) {
			int value = rnd.nextInt(population.getScore());
			int sum = 0;
			for (Specimen specimen : population.getPopulation()) {
				sum += specimen.getScore();
				if (value < sum) {
					specimens[i] = specimen;
					break;
				}
			}
		}
		return specimens;
	}

}